﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Medianamik.UI.Web.TypeManagement.Controls;
using Medianamik.Core;

namespace Medianamik.Tests.Unit.Medianamik.UI.Web.TypeManagement.Controls
{
    [TestFixture]
    public class TemplatePickerTest
    {
        private const string DefaultNodeName = "Rambo";
        [Test]
        public void TemplatePicker_should_display_warning_when_TypeTemplate_associated_PageTemplate_is_null()
        {
            var templatePicker = new TemplatePickerFake();
            var template = new TypeTemplateWithoutPageTemplate();
            templatePicker.Template = template;
            var expectedWarning = TemplatePicker.MissingPageTemplateWarningMessage;

            templatePicker.Page_Load(null, null);

            Assert.That(templatePicker.TemplateName, Is.EqualTo(expectedWarning));
        }

        [Test]
        public void Should_display_Website_name_and_PageTemplate_name()
        {
            var templatePicker = new TemplatePickerFake();
            var template = new WorkingTypeTemplate();
            templatePicker.Template = template;
            var expectedTemplateName = DefaultNodeName + " --> " + DefaultNodeName;

            templatePicker.Page_Load(null, null);

            Assert.That(templatePicker.TemplateName, Is.EqualTo(expectedTemplateName));
        }




        public class TemplatePickerFake : TemplatePicker
        {
            public override TypeTemplate Template
            {
                get;
                set;
            }

            protected internal override string TemplateName
            {
                get;
                set;
            }
        }

        public class TypeTemplateWithoutPageTemplate : WorkingTypeTemplate
        {
            protected override PageTemplate GetAssociatedPageTempate()
            {
                return null;
            }
        }

        public class WorkingTypeTemplate : TypeTemplate
        {
            private T GetAssociated<T>() where T : NodeWrapper, new()
            {
                var innerNode = new FakeNode { NamePropertyId = Guid.NewGuid() };
                var wrapper = new T { InnerNode = innerNode };

                return wrapper;
            }
            protected override WebSite GetAssociatedWebsite()
            {
                return GetAssociated<WebSite>();
            }

            protected override PageTemplate GetAssociatedPageTempate()
            {
                return GetAssociated<PageTemplate>();
            }
        }

        public class FakeNode : Node
        {
            public override string DisplayName
            {
                get
                {
                    return DefaultNodeName;
                }
            }

        }
    }
}